#include "main.h"
void cheoHoa(int a[][MAX], int n)
{
	int c, c1, c2, d1, d2, d3, d, e;	
	//Tinh dinh thuc
	c1 = a[0][0] * a[1][1] * a[2][2] + a[0][1] * a[1][2] * a[2][0] + a[1][0] * a[2][1] * a[0][2];
	c2 = a[0][2] * a[1][1] * a[2][0] + a[0][1] * a[1][0] * a[2][2] + a[1][2] * a[2][1] * a[0][0];
	c = c1 - c2;
	d1 = (a[0][0] + a[1][1])*a[2][2];
	d2 = a[0][0] * a[1][1];
	d3 = a[0][1] * a[1][0] + a[0][2] * a[2][0] + a[1][2] * a[2][1];
	d = d1 + d2 - d3;
	e = a[0][0] + a[1][1] + a[2][2];
	int b[3], f = 0, du;
	for (int i = -10; i <= 10; i++)
	{
		if (-1 * (i*i*i) + e*(i*i) - d*i + c == 0)
		{
			b[f] = i;
			f++;
		}
	}
	du = f;
	for (int i = 0; i < du; i++)
		cout << "\t" << "Lamda " << i + 1 << " = " << b[i] << endl;
	cout << endl;
	int a2[MAX][MAX], a3[MAX][MAX], t = 0, k, x, flag = 0, y;
	copy(a, a3, n);
	for (int i = 0; i < du; i++)
	{
		copy(a, a2, n);
		cout << endl << "\t" << "Lamda = " << b[i] << endl;
		truLamda(a2, b[i], n);
		cout << endl;
		xuat(a2, n);
		cout << endl;
		bacThang(a2, n);
		cout << endl;
		if (a2[1][1] != 0 || a2[1][2] != 0)
		{
			cout << "\t" << "Nghiem don !" << endl;
			y = b[i];
			t++;
		}
		else if (a2[1][1] == 0 && a2[1][2] == 0)
		{
			flag++;
			cout << "\t" << "Nghiem kep !" << endl;
			x = b[i];
			t += 2;
		}
		else if (a2[2][2] != 0)
		{
			cout << "\t" << "Khong co nghiem !" << endl;
			b[i] = 0;
		}
	}
	cout<<"\n\n\t\t-------------------------------------";
	if (flag)
	{
		for (int l = 0; l < n; l++)
		{
			if (b[l] && b[l] != x)
			{
				a3[2][2] = y;
				a3[0][0] = x;
				a3[1][1] = x;
			}
		}
	}
	else
	{
		for (int i = 0; i<n; i++)
			a3[i][i] = b[i];
	}
	if (t == 3)
	{
		cout << endl << "\t" << "======>MA TRAN CHEO HOA<======" << endl << endl;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
			if (i != j)
				a3[i][j] = 0;
			for (int i = 0; i < n; i++)
			{
				cout << endl;
				//cout << endl << "|-----------------------------------------------|" << endl << "|\t\t|\t\t|\t\t|" << endl << "|";
				for (int j = 0; j < n; j++)
				{
					//cout << "\t" << a3[i][j] << "\t|";
					cout << "\t" << a3[i][j] << "\t";
				}
				cout << endl;
				//cout << endl << "|\t\t|\t\t|\t\t|";
			}
		//cout << endl << "|-----------------------------------------------|" << endl;
	}
	else cout << endl << "\t" << "Khong co cheo hoa" << endl << endl;
}